<?php 
session_start();
if($_SESSION['sess_id'] <> session_id()){
?>
<SCRIPT LANGUAGE="JavaScript">
	alert("กรุณาเข้าสู่ระบบให้ถูกต้องตามขั้นตอน...!");
		location="login.php";
</script>
<?php }?>
<?php
if($_SESSION['SETID'] != $_GET['set_id']){
//if( ! session_is_registered("RECORD_PAGE") ){
//session_register("RECORD_PAGE");
$_SESSION['SETID'] = $_GET['set_id'];
require_once('../include/connect.inc.php');
require_once('../include/layout-jquery.php');
	$time_now = date('Y-m-d H:i:s');
		$query = query("SELECT * FROM tb_set_of_test where set_id='".$_GET['set_id']."'");
			while ($rs = fetch_array($query)){
				$set_id			    			= $rs['set_id'];
				$set_time_dotest    		= $rs['set_time_dotest'];
				$_SESSION['set_id'] 	= $rs['set_id'];
	 			$times1						= date('Y-m-d H:i:s ',strtotime("$time_now"));
	 			$times_nows				= date('m-d-Y H:i:s ',strtotime("$times1 + $set_time_dotest minutes"));
				
				$h=floor($set_time_dotest/60);
				if($h==0){
					$min=$set_time_dotest;
				}else{
					$min=$set_time_dotest%($h*60);
				}
				if($h<=9){
			   		$h="0".$h;
				}
				if($min<=9){
			   		$min="0".$min;
				}
				$set_time_dotest=$h.":".$min.":00";
	//$_SESSION['TIME_JAVA'] = $set_time_dotest;
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Model Test Program</title>
<link rel="stylesheet" type="text/css" href="../template/template_style_wood.css">
<script language="JavaScript">
    <!--
    var message="ห้ามคลิ๊กขวานะครับ!!!"; // กำหนดตัวแปรและค่าสำหรับข้อความที่แสดงเมื่อมีการคลิกขวา
    function click(e)
    {
    	if (document.all) {
    		if (event.button == 2) {
    			alert(message);
    			return false;
    		}
    	}
    	if (document.layers) {
    		if (e.which == 3) {
    			alert(message);
    			return false;
    		}
    	}
    }
    if (document.layers) {
    	document.captureEvents(Event.MOUSEDOWN);
    }
    document.onmousedown=click;
    //
</script>
</head>
<body>
<div id="menu"><div><img src="../images/icon-m.png" /></div>
<div id="menulinks">
</div></div>
 <div id="p3">    
<div id="header"></div>
<div id="page"><div id="p1">คุณ<?php echo $_SESSION['student_name'];?> <hr />
<font color="#FF0000" size="+3">ยินดีต้อนรับสู่ระบบทำข้อสอบ ขอให้โชคดีในการสอบทุกท่าน...  </font>
<script type="text/javascript">
var limit="<?=$set_time_dotest?>";
if (document.images){
	var parselimit = limit.split(":")
		parselimit = parselimit[0]*60*60+parselimit[1]*60+parselimit[2]*1;
}
function begintimer(){
	if (!document.images)
		return;
	if (parselimit == 1){
		alert("เสียใจด้วยค่ะ หมดเวลาทำข้อสอบแล้วค่ะ !!!");
		$('#form1').submit();
	}
	else{
	parselimit -= 1;
	curmin=Math.floor(parselimit / 60);
	cursec=parselimit%60;
	curhour= Math.floor(curmin / 60);
	if(curhour != 0 )
		curtime="เหลือเวลา  <font color=red> "+curhour+" </font> ชั่วโมง <font color=red> "+(curmin % 60)+" </font> นาที  <font color=red>"+cursec+" </font> วินาที ";
	else
	if (curmin != 0 ){
		curtime="เหลือเวลา <font color=red> "+curmin+" </font> นาที <font color=red>"+cursec+" </font> วินาที ";
	}
	else{
		if(cursec == 0){
		alert('หมดเวลาทำข้อสอบ');
		}
		else{
			curtime="เหลือเวลา <font color=red>"+cursec+" </font> วินาที ";
		}
	}
		document.getElementById('showRemain').innerHTML = curtime;
		setTimeout("begintimer()",1000)
	}
} 
</script>
<input type="hidden" name="show_time"  id="show_time" value="<?="$times_nows"?>" />
</div>
<div id="p1">
<div id="showRemain" style="font-family:Arial, Helvetica, sans-serif; font-size:14px; color:#FF0000;">
</div>
</div>
<form id="form1" name="form1" method="post" action="save.php">
<input type="hidden" name="set_id" id="set_id" value="<?php echo $set_id;?>">
<?php
//เลือกแสดงชุดข้อสอบ จากค่าID ชุดที่เลือกมา
$number=0;
$count=0;
$query = query("SELECT * FROM tb_set_detail where set_id='".$_GET['set_id']."' order by match_set_cho DESC");
	while ($rs = fetch_array($query)){
		$match_set_cho=$rs['match_set_cho'];
		$question_id=$rs['question_id'];
?>

<?php if ($rs['match_set_cho']!=""){?>
<div id="p1">
<font size="+2" color="#663300"><strong>ข้อสอบจับคู่!</strong></font>
<table border="0"  id="table-t">
    <tr>
      <td width="187"><strong>คำถาม</strong></td>
      <td width="198"><strong>คำตอบ</strong></td>
    </tr>
<?php 
$num=0;
	$queryQ=query("SELECT * FROM tb_question where match_set=$match_set_cho order by RAND()");
		while ($rsQ = fetch_array($queryQ)) {
			$ques_match_id=$rsQ['question_id'];
			$ques_match_detail=$rsQ['question_detail'];
			$type_id=$rsQ['type_id'];
			$num++;
?>
    <tr>
    	<input type="hidden" name="type[]" id="type[]" value="<?php echo "$type_id";?>">
    	<input type="hidden" name="q_id[]" id="q_id[]" value="<?php echo "$ques_match_id";?>">
		<td>ข้อที่ <?php echo "$num";?>)<?php echo "$ques_match_detail";?></td>
      	<td>
      	  <select name="choice[]<?=$count?>" id="choice[]<?=$count?>">
          <option value="0">---เลือกคำตอบ---</option>
<?php 
	$queryC=query("SELECT * FROM tb_choice where match_set=$match_set_cho order by RAND()");
		while ($rsC = fetch_array($queryC)) {
			$choice_id=$rsC['choice_id'];
			$choice_detail=$rsC['choice_detail'];
?>
          <option value="<?php echo "$choice_id";?>"><?php echo "$choice_detail";?></option>
<?php }?>
   	      </select>
       </td>
    </tr>
<?php }?>
</table>
</div>
<?php }?>

<?php
//เลือกแสดงข้อสอบ ตามID ของรายละเอียดชุด
$query2= query("SELECT * FROM  tb_question where question_id='".$question_id."' ");
	while ($rs2= fetch_array($query2)) {
		$question_id2=$rs2['question_id'];
		$question_detail=$rs2['question_detail'];
		$type_id=$rs2['type_id'];
		$number++;
?>
<div id="p1">
<?php 
$queryT= query("SELECT * FROM  tb_type where type_id='$type_id' ");
	while ($rsT= fetch_array($queryT)){
		$type=$rsT['type_name'];
?>
<font size="+2" color="#663300"><strong>ข้อสอบประเภท"<?php echo "$type";?>"</strong></font>
<?php }?>
<table border="0"  id="table-t">
	<tr>
		<td>
		<div>
		<input type="hidden" name="type[]" id="type[]" value="<?=$rs2[type_id] ?>">
		<input type="hidden" name="q_id[]" id="q_id[]" value="<?=$rs2[question_id] ?>">
ข้อที่ <? echo "$number"; echo ". ";?><? echo $question_detail; ?></div>
<?php
$query3 = query("SELECT * FROM tb_choice  where question_id='".$rs2['question_id']."'order by RAND()");
	while ($rs3 = fetch_array($query3)){
	?>
<div class="p21">
<?php  if($rs2['type_id']==4){?>
	<input type="text" name="answord[]<?=$count?>" id="answord[]" />
<?php }
	if($rs2['type_id'] != 5){
	if($rs2['type_id'] != 7){
	if($rs2['type_id'] != 4){
	if($rs2['type_id'] == 2){
?>
	<label>
    	<input type="checkbox" name="choice[]<?=$count?>"  value="<? echo $rs3[choice_id];?>" />
    </label>
<?php }else{ ?>
	<label>
		<input type="radio" name="choice[]<?=$count?>"  value="<? echo $rs3[choice_id];?>" />
	</label>
<?php }?>
<?php  echo $rs3['choice_detail'];?>
<?php }?>
	<?php }?>
		<?php }?>
	</div>
<?php }?>
	<th width="37%" align="left">
<?php  if($rs2['pic_name']!=""){?>
	<div align="center"><a href="../upload/<? echo  $rs2['pic_name'];?>" target="_blank">
	<img src="../upload/<?php echo  $rs2['pic_name'];?>" border="0" width=100 height=100 /></a>
	<div><small>คลิ๊กที่รูปเพื่อดูขนาดใหญ่</small></div>
	</div>
<? }?>
<?php  if($rs2['media_name']!=""){?>
<div align="center">
	<object id="mediaPlayer" height="23" width="187" classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95">
		<param name="fileName" value='<?php echo "../upload/".$rs2['media_name']; ?>' valuetype='ref' />
		<param name="AutoStart" value="0" />
		<param name="TransparentAtStart" value="true" />
		<param name="SendPlayStateChangeEvents" value="true" />
		<param name="ShowControls" value="0" />
		<param name="ShowDisplay" value="0" />
		<param name="ShowStatusBar" value="1" />
		<param name="ShowGotoBar" value="0" />
		<param name="ShowPositionControls" value="0" />
		<param name="AutoSize" value="0" />
		<param name="volume" value="0" />
		<param name="loop" value="0" />
	</object>
<div align="center">
	<img style="CURSOR: pointer" 
	onclick="mediaPlayer.play()" height="30" 
	src="../soundbox/play-48x48.png" 
	width="30" /><img style="CURSOR: pointer" 
	onclick="mediaPlayer.pause()" height="30" 
	src="../soundbox/puse-48x48.png" 
	width="30" /><img 
	src="../soundbox/stop-48x48.png" 
	width="30" height="30" style="CURSOR: pointer" 
	onclick="mediaPlayer.stop()" />
</div>
</div>
<? }?>
		</th>
	</tr>
</table>
</div>
<? }?>
<? $count++;?> 
<? }?>
<? }?>
<script>begintimer()</script>
<div id="p1"><input type="submit" name="save" id="save" value="ส่งข้อสอบ" onClick="return confirm('ยืนยันการส่งข้อสอบ')" /></div>
</form>
    </div>
<div id="footer"></div>
</div>
</body>
</html>
<?php
}else{//case  refresh page
?>
<script type="text/javascript">alert('Don\'t refresh');</script>
<?php }?>